﻿define([
    'require',
    'angular',
    'underscore'
], function (require, angular, _) {
    return function ($scope, $routeParams, $http, RuleSerivce,$dataSourceManager,$timeout,Resource,$compile,$rootScope, GillionMsg) {
        getList();
        var dataSouceResource = Resource("/grule-manager/dataSource/:id", {
            id: "@id"
        });
        var sqlConfigResource = Resource("/grule-manager/sqlConfig/:id", {
            id: "@id"
        });
        function getList(){
            Resource("/grule-manager/dataSource/datasource_list/:id", {id: '@id'}).get({
                id:  $routeParams.projectId
            }, function (response) {
                $scope.dataSources =response.data;
                $timeout(function () {
                    if ($scope.dataSources.length > 0) {
                        $scope.viewDataSource($scope.dataSources[0]);
                    }
                },200)
            });
        }

        $scope.$on("unitCaseList", function (context, dataSource) {
            $scope.unitcases =dataSource.records;
            //默认选中首个
            $timeout(function () {
                if ($scope.unitcases.length > 0) {
                    $scope.viewCase($scope.unitcases[0].id, $scope.unitcases[0].unitCaseName);
                }
            },200);
        });

        $scope.addDataSource=function (){
            $scope.dlg = GillionMsg.showUrl({
                title: '新建数据源',
                url: '/html2/component/jdbc/dataSourceManager',
                data:{projectId:$routeParams.projectId,projectCode:$scope.currProjcet.projectCode},
                height:300,
                width:400,
                onClose: function(){
                    getList();
                }
            });
        };

        $scope.saveJdbc=function(){
            Resource("/grule-manager/dataSource", {}).update($scope.dataSource,function(response){
                if (response.success) {
                    GillionMsg.alert('提示', "保存成功！");
                }
            })
        };

        $scope.addSqlConfig=function (){
            $scope.dlg = GillionMsg.showUrl({
                title: '新建sql配置',
                url: '/html2/component/jdbc/sqlConfigManager',
                data:{datasourceId: $scope.datasourceId},
                height:400,
                width:700,
                onClose: function(){
                    if (undefined !== $scope.datasourceId) {
                        var param = {};
                        param.datasourceId=$scope.datasourceId;
                        $dataSourceManager.dataSources["SqlConfigSource"].params = param;
                        $dataSourceManager.dataSources["SqlConfigSource"].doRequestData();
                    }
                }
            });
        };
        $scope.editSqlConfig=function (row){
            $scope.dlg = GillionMsg.showUrl({
                title: '修改sql配置',
                url: '/html2/component/jdbc/sqlConfigManager',
                data:row,
                height:400,
                width:700,
                onClose: function(){
                    var param = {};
                    param.datasourceId=$scope.datasourceId;
                    $dataSourceManager.dataSources["SqlConfigSource"].params = param;
                    $dataSourceManager.dataSources["SqlConfigSource"].doRequestData();
                }
            });
        };




        $scope.deleteDataSource=function (id){
            dataSouceResource.delete({
                id: id
            }, function (response) {
                if(response.success){
                    getList();
                }
            })
        }

        $scope.viewDataSource=function(dataSource){
            var list = document.getElementsByName("dataSourceList");
            for(var i=0; i< list.length; i++){
                list[i].className = "engine-test-case";
            }
            var id =dataSource.id;
            document.getElementById(id).setAttribute("class","engine-test-case  engine-test-case-on");


            $scope.datasourceName = dataSource.datasourceName;
            $scope.dataSource = dataSource;
            $scope.datasourceId =dataSource.id;
            var param = {};
            param.datasourceId=$scope.datasourceId;
            $dataSourceManager.dataSources["SqlConfigSource"].params = param;
            $dataSourceManager.dataSources["SqlConfigSource"].doRequestData();
        }

        $scope.removeSqlConfig=function(sqlConfig){
            GillionMsg.confirm("提示信息","是否确定删除?",function(r){
                if(r){
                    sqlConfigResource.delete({
                        id:  sqlConfig.id
                    }, function () {
                        var param = {};
                        param.datasourceId=$scope.datasourceId;
                        $dataSourceManager.dataSources["SqlConfigSource"].params = param;
                        $dataSourceManager.dataSources["SqlConfigSource"].doRequestData();
                    });
                }
            })


        }


    };
});